﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// 产生发货单号码，并设定发货信息
    /// </summary>
    public partial class TWBVV03
    {
        double a = 0; double b = 0; int c = 0;
        ITXEXRPT ITXEXRPT;

        public TWBVV03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            MESSAGES += "WBUNREQ;"; 
            InitializeComponent();
        } 
         
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
            if (dwMaster == null)
            {
                TAX = "";
            }
            else
            {
                TAX = dwMaster["WBBTAX"].ToString();
            }
        }

        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            { 
                case "WBUNREQ":
                    TBMain.Clear();
                    break;
            }
        } 

        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (LIEFSCH.Trim().Length == 0)
            {
                MessageBox.Show("发货单号尚未指定");
                return;
            } 
            TMV.VV03();
            string key = ORDERNUM;
            VVT = "";
            VVT = key;
        }
        
        bool IsWB62 = false;

        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            base.bgWorker_DoWork(sender, e);
            EventDispatcher.Instance.RaiseStatus("开始分批", StatusEventType.StopProgress); 
            if (IsWB62)
            {
                TMV.WBFREE(true);
            }
        }

        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.bgWorker_RunWorkerCompleted(sender, e);
            string key = ORDERNUM;
            VVT = "";
            VVT = key;
        }

        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {  
            WBROWS = new List<DataRow>();
            DataTable TB = TBMain.Clone();
            foreach (var item in dlgMaster.SelectedItems)
            {
                if (!item.GetType().ToString().Contains("DataRowView")) continue;

                DataRow _dwWB = ((System.Data.DataRowView)item).Row;
                if (_dwWB["WBBURLIEFSCH"].ToString() == "99999999" || _dwWB["WBBURLIEFSCH"].ToString() == "00000000")
                {
                    continue;
                }
                dwData=TB.Rows.Add();
                dwData.ItemArray = _dwWB.ItemArray; 
            }
            foreach (DataRow _dwWB in TB.Rows)
            {
                WBROWS.Add(_dwWB);
            }
            TMV.VVTFREE();
            //VVT = "";
            //VVT = ORDERNUM;
        }

        private void WBB62Command(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                {
                    MessageBox.Show("请先选择任意一个布卷");
                    return;
                }
                IsWB62 = true; 
                WBROWS = new List<DataRow>();
                DataTable TB = TBMain.Clone();
                foreach (var item in dlgMaster.SelectedItems)
                {
                    if (!item.GetType().ToString().Contains("DataRowView")) continue;
                    DataRow _dwWB = ((System.Data.DataRowView)item).Row;

                    if (_dwWB["WBBURLIEFSCH"].ToString() != "99999999" && _dwWB["WBBURLIEFSCH"].ToString() != "00000000")
                    {
                        MessageBox.Show("请先取消" + dwMaster["WBBSTKNR"].ToString() + "发货批次");
                        break;
                    }
                    WBROWS.Add(_dwWB);
                }

                if (WBROWS.Count == 0)
                    return;
                bgWorker.RunWorkerAsync(); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void PrintLBCommand(object sender, RoutedEventArgs e)
        {
            if (ITXEXRPT == null)
            {
                ITXEXRPT = new WPFExcelReport.ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
            }
            ITXEXRPT.dsSource = new DataSet();
            bool IsFound = false;
            if (TBMain.Rows.Count == 0)
                return;
            DataTable tb = TBMain.Clone();
            foreach (DataRowView item in dlgMaster.SelectedItems)
            {
                DataRow _dwWB = tb.Rows.Add();
                _dwWB.ItemArray = item.Row.ItemArray;
            }
            switch (app.company)
            {
                default:
                case "JLT":
                    ITXEXRPT.dsSource.Merge(tb);
                    ITXEXRPT.DocType = "JLTWBD10IT";
                    foreach (DataRow row in tb.Rows)
                    {
                        string PACKNR = row["WBBPACKNR"].ToString();
                        if (PACKNR == "0000")
                            continue;
                        string art = row["WBBART"].ToString().Trim();
                        string dr = @"C:\Program Files\mabu\templates";
                        string filename = "JLB" + art.Substring(3, 4) + ".xls";
                        if (!System.IO.Directory.Exists(dr))
                        {
                            IsFound = false;
                        }
                        else
                        {
                            string[] files = System.IO.Directory.GetFiles(dr);
                            foreach (string file in files)
                            {
                                if (!file.Contains(filename)) continue; IsFound = true; ITXEXRPT.strExcelFile = file; break;
                            }
                            if (!IsFound)
                            {
                                filename = "coillabel" + art.Substring(3, 4) + ".xls";
                                foreach (string file in files)
                                {
                                    if (!file.Contains(filename)) continue; IsFound = true; ITXEXRPT.strExcelFile = file; break;
                                }
                            }
                        }
                    }
                    if (IsFound) ITXEXRPT.WriteExcelReport();
                    break;
            }
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {

            if (!IsEdit)
            {
                MV.TBTemp = TBMain.Copy();
            }
            else
            {
                MV.TBTemp = null;
            }
            base.EditCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            TMV.BATCHWBCHG();
            base.SaveCommand(sender, e);
        }
         
        public override void LOADBYAUF()
        {
            MV.Counts1 = 0;
            MV.SUM1 = 0;
            MV.Counts2 = 0;
            MV.SUM2 = 0;
            a = 0; b = 0; Counts = 0; 

            if (ORDERNUM.Length < 20)
            {
                TBMain.Clear();
                return;
            } 
            LIEFSCH = ORDERNUM.Substring(12, 8);
            PM["AUFTYP"] = ORDERNUM.Substring(0, 1);
            PM["KRS"] = ORDERNUM.Substring(1, 2);
            PM["LFD"] = ORDERNUM.Substring(3, 6);
            PM["POS"] = ORDERNUM.Substring(9, 3);
            PM["LIEFSCH"] = ORDERNUM.Substring(12, 8);
            PM["LO"] = LO;
            PM["LB"] = LB;
            TMV.VVTLOAD(PM); 
            vwMaster.View.MoveCurrentToFirst(); 
            base.LOADBYAUF();
            foreach (DataRow dWB in TBMain.Rows)
            {
                a += Helpers.Helper.RoundX(Convert.ToDouble(dWB["WBBISTKLG"].ToString()) * 0.001,100);
                b += Helpers.Helper.RoundX(Convert.ToDouble(dWB["WBBISTKLG"].ToString()) * 0.001 / 0.9144,10); 
            }
            MSUM.Text = a.ToString();
            YSUM.Text = b.ToString();
            CSUM.Text = Counts.ToString();
            
        }

        public void MoveSelectedCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                filter = "WBBURLIEFSCH='00000000' OR WBBURLIEFSCH='99999999' ";
                rows = TBMain.Select(filter);
                if(rows.Length>0)
                {
                    MessageBox.Show("先指定发货批次");
                    return;
                }
                foreach (DataRow _dwWB in TBMain.Rows)
                {
                    if (_dwWB["WBBLB"].ToString() == LO)
                    {
                        continue;
                    }           
                     
                    TMV.MOVE(_dwWB, "01", "CW", "01      ", this.RUNID);
                    TargetMTR += Helpers.Helper.RoundX(Convert.ToDouble(_dwWB["WBBISTKLG"].ToString()) / 1000, 100);
                }
                TMV.VWBW.POSTCHG();
                TBMain.Clear();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
       
    }
}
